package com.seed.core.configuration;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.seed.core.converter.ActionConverter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class AppConfiguration extends MicroConfiguration {
    private String name;
    private String owner;
    private String version;
    private String logo;
    private String copyright;
    private boolean enableTenant;

    private Map<String, MicroConfiguration> micros;
    private Map<String, GroupConfiguration> groups; // 对【seed,action,home】进行分组的定义
    private String root; // 前端用到的app根组件名称
    private LayoutConfiguration rootLayout;
    private Map<String, Object> rootProps; // app根组件对应的属性
    private String login; // 前端用到的login组件名称
    private LayoutConfiguration loginLayout;
    private Map<String, Object> loginProps; // login组件对应的属性
    private String home; // 前端用到的home组件名称
    private LayoutConfiguration homeLayout;
    private Map<String, Object> homeProps; // home组件对应的属性
    private String error; // 前端用到的error组件名称
    private LayoutConfiguration errorLayout;
    private Map<String, Object> errorProps; // error组件对应的属性
    private String redirect; // 前端用到的redirect组件名称
    private LayoutConfiguration redirectLayout;
    private Map<String, Object> redirectProps; // redirect组件对应的属性

//    public App toApp() {
//        App ret = new App();
////        ret.setName(this.name);
////        ret.setOwner(this.owner);
//        ret.setConfiguration(this);
//
//        if(this.micros != null) {
//            Map<String, Micro> micros = new HashMap<>();
//            for(Map.Entry<String, MicroConfiguration> entity: this.micros.entrySet()) {
//                String microName = entity.getKey();
//                Micro micro = entity.getValue().toMicro();
//                micro.setName(microName);
//                micros.put(microName, micro);
//            }
////            ret.setMicros(micros);
//        }
//
//        if(this.seeds != null) {
//            Map<String, Seed> seeds = new HashMap<>();
//            for(Map.Entry<String, SeedConfiguration> entry: this.seeds.entrySet()) {
//                String seedName = entry.getKey();
//                Seed seed = entry.getValue().toSeed();
//                seed.setName(seedName);
//                seeds.put(seedName, seed);
//            }
////            ret.setSeeds(seeds);
//        }
//
//        if(this.actions != null) {
//            Map<String, Action> actions = new HashMap<>();
//            for(Map.Entry<String, ActionConfiguration> entry: this.actions.entrySet()) {
//                String actionName = entry.getKey();
//                Action action = entry.getValue().toAction();
//                action.setName(actionName);
//                actions.put(actionName, action);
//            }
////            ret.setActions(actions);
//        }
//
////        if(libs != null) ret.setLibs(List.of(libs));
//
//        return ret;
//    }

    public AppConfiguration toFront() {
        AppConfiguration app = new AppConfiguration();
        app.setName(name);
        app.setOwner(owner);
        app.setLabel(label);
        app.setComment(comment);
        app.setVersion(version);
        app.setLogo(logo);
        app.setCopyright(copyright);
        app.setEnableTenant(enableTenant);
        app.setComponent(component);
        app.setLayout(layout);
        app.setComponentProps(componentProps);
        if(micros != null) {
            Map<String, MicroConfiguration> frontMicros = new HashMap<>();
            for(Map.Entry<String, MicroConfiguration> entry: micros.entrySet()) {
                frontMicros.put(entry.getKey(), entry.getValue().toFront());
            }
            app.setMicros(frontMicros);
        }

        if(seeds != null) {
            Map<String, SeedConfiguration> frontSeeds = new HashMap<>();
            for(Map.Entry<String, SeedConfiguration> entry: seeds.entrySet()) {
                frontSeeds.put(entry.getKey(), entry.getValue().toFront());
            }
            app.setSeeds(frontSeeds);
        }

        if(actions != null) {
            Map<String, ActionConfiguration1> frontActions = new HashMap<>();
            for(Map.Entry<String, ActionConfiguration1> entry: actions.entrySet()) {
                frontActions.put(entry.getKey(), ActionConverter.toFront(entry.getValue()));
            }
            app.setActions(frontActions);
        }

        app.setGroups(groups);
        app.setRoot(root);
        app.setRootLayout(rootLayout);
        app.setRootProps(rootProps);
        app.setLogin(login);
        app.setLoginLayout(loginLayout);
        app.setLoginProps(loginProps);
        app.setRedirect(redirect);
        app.setRedirectLayout(redirectLayout);
        app.setRedirectProps(redirectProps);
        app.setError(error);
        app.setErrorLayout(errorLayout);
        app.setErrorProps(errorProps);
        app.setHome(home);
        app.setHomeLayout(homeLayout);
        app.setHomeProps(homeProps);
        app.setHomes(homes);
        app.setGroups(groups);
        app.setLibs(libs);

        return app;
    }

//    public MicroConfiguration microIf(String microName) {
//        if(micros == null) return null;
//
//        return micros.get(microName);
//    }
//
//    public MicroConfiguration micro(String microName) {
//        MicroConfiguration micro = microIf(microName);
//        if(micro == null) throw new RuntimeException(String.format("应用【%s】下的服务【%s】不存在", name, microName));
//
//        return micro;
//    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

//    public String getLabel() {
//        return label;
//    }
//
//    public void setLabel(String label) {
//        this.label = label;
//    }
//
//    public String getComment() {
//        return comment;
//    }
//
//    public void setComment(String comment) {
//        this.comment = comment;
//    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getLogo() {
        return logo;
    }

    public void setLogo(String logo) {
        this.logo = logo;
    }

    public String getCopyright() {
        return copyright;
    }

    public void setCopyright(String copyright) {
        this.copyright = copyright;
    }

    public boolean isEnableTenant() {
        return enableTenant;
    }

    public void setEnableTenant(boolean enableTenant) {
        this.enableTenant = enableTenant;
    }

    public Map<String, MicroConfiguration> getMicros() {
        return micros;
    }

    public void setMicros(Map<String, MicroConfiguration> micros) {
        this.micros = micros;
    }

    public Map<String, GroupConfiguration> getGroups() {
        return groups;
    }

    public void setGroups(Map<String, GroupConfiguration> groups) {
        this.groups = groups;
    }

    public String getRoot() {
        return root;
    }

    public void setRoot(String root) {
        this.root = root;
    }

    public LayoutConfiguration getRootLayout() {
        return rootLayout;
    }

    public void setRootLayout(LayoutConfiguration rootLayout) {
        this.rootLayout = rootLayout;
    }

    public Map<String, Object> getRootProps() {
        return rootProps;
    }

    public void setRootProps(Map<String, Object> rootProps) {
        this.rootProps = rootProps;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public LayoutConfiguration getLoginLayout() {
        return loginLayout;
    }

    public void setLoginLayout(LayoutConfiguration loginLayout) {
        this.loginLayout = loginLayout;
    }

    public Map<String, Object> getLoginProps() {
        return loginProps;
    }

    public void setLoginProps(Map<String, Object> loginProps) {
        this.loginProps = loginProps;
    }

    public String getRedirect() {
        return redirect;
    }

    public void setRedirect(String redirect) {
        this.redirect = redirect;
    }

    public LayoutConfiguration getRedirectLayout() {
        return redirectLayout;
    }

    public void setRedirectLayout(LayoutConfiguration redirectLayout) {
        this.redirectLayout = redirectLayout;
    }

    public Map<String, Object> getRedirectProps() {
        return redirectProps;
    }

    public void setRedirectProps(Map<String, Object> redirectProps) {
        this.redirectProps = redirectProps;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public LayoutConfiguration getErrorLayout() {
        return errorLayout;
    }

    public void setErrorLayout(LayoutConfiguration errorLayout) {
        this.errorLayout = errorLayout;
    }

    public Map<String, Object> getErrorProps() {
        return errorProps;
    }

    public void setErrorProps(Map<String, Object> errorProps) {
        this.errorProps = errorProps;
    }

    public String getHome() {
        return home;
    }

    public void setHome(String home) {
        this.home = home;
    }

    public LayoutConfiguration getHomeLayout() {
        return homeLayout;
    }

    public void setHomeLayout(LayoutConfiguration homeLayout) {
        this.homeLayout = homeLayout;
    }

    public Map<String, Object> getHomeProps() {
        return homeProps;
    }

    public void setHomeProps(Map<String, Object> homeProps) {
        this.homeProps = homeProps;
    }
}
